import Dialog from "./Dialog.vue";
import {h, render} from "vue";

interface modalOptions {
    title: string,
    comps: string,
    params?: any,
    parentContext?: any,
    width?: string,
    height?: string,
    headerComps?:string,
    footButtons?:Object,
}

let createMount = (opts: any) => {
    const mountNode = document.createElement("div");
    document.body.appendChild(mountNode);
    const node = h(Dialog, {
        ...opts,
        modelValue: true,
        remove() {
            document.body.removeChild(mountNode);
        },
    });
    node.appContext = Modal._context;
    render(node, mountNode);
};
const Modal = {
    install(app: any, options: any) {
        app.config.globalProperties.$Modal = {
            // @ts-ignore
            show: (opts: modalOptions) => {
                //   options.id = options.id || "modal_show_" + 1; //唯一id 删除组件时用于定位
                createMount({
                    ...opts,
                    readOnly: true
                });
            },
            // @ts-ignore
            edit: (opts: modalOptions) => {
                createMount({
                    ...opts,
                    readOnly: false
                });
            },
        };
    },
    _context: null,
};
export default Modal;